package m.jclecai.com.adapter.holder;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.math.BigDecimal;
import java.math.RoundingMode;

import m.jclecai.com.R;

import butterknife.BindView;
import m.jclecai.com.bean.GroupBuyBean;
import m.jclecai.com.utils.BitmapUtil;
import m.jclecai.com.utils.ScreenUtil;
import m.jclecai.com.view.CircularProgressBar;

/**
 * Created by ZD on 2017/5/15.
 */

public class GroupBuyHolder extends ViewHolder{
    @BindView(R.id.cb_progress)
    CircularProgressBar cbProgress;
    @BindView(R.id.tv_progress)
    TextView tvProgress;
    @BindView(R.id.tv_buy)
    TextView tvBuy;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_count)
    TextView tvCount;
    @BindView(R.id.tv_people)
    TextView tvPeople;
    @BindView(R.id.llGif)
    LinearLayout llGif;
    String[] stars,medals,cups,crowns;

    public GroupBuyHolder(Context context, @LayoutRes int resource) {
        super(context, resource);
        if(stars == null)
            stars = this.initLevel("star", "星星");
        if(medals == null)
            medals = this.initLevel("medal", "奖牌");
        if(cups == null)
            cups = this.initLevel("cup", "金杯");
        if(crowns == null)
            crowns = this.initLevel("crown", "皇冠");
    }

    public void init(GroupBuyBean bean){
        if(!bean.isPlayAnimation()){
            Animation animation = AnimationUtils.loadAnimation(getContext(),R.anim.slide_in_from_bottom);
            animation.setDuration(300);
            startAnimation(animation);
            bean.setPlayAnimation(true);
        }
        float progress = bean.getProgress();
        BigDecimal bg = new BigDecimal(progress).setScale(2, RoundingMode.UP);
//        if(progress < 0)
//            progress = 0;
//        if(progress > 100)
//            progress = 100;
        cbProgress.setProgressWithAnimation(progress, 500); // Default duration = 1500ms
        tvTitle.setText(bean.getUsername());
        tvType.setText(bean.getType());
        tvMoney.setText(bean.getOne_money());
        tvCount.setText(String.valueOf(bean.getTeambuy_left()));
        tvPeople.setText(bean.getJoins());
        tvProgress.setText(bg.floatValue()+"%\n进度");
        tvBuy.setText("已预购"+bean.getProgress_loss()+"%");
        llGif.removeAllViews();
        addGifView(bean.getUser_winmoneys());
    }

    public void addGifView(float money){
        int width = ScreenUtil.dip2px(getContext(),13);
        String level = getLevel(money);
        if(!TextUtils.isEmpty(level)){
            String[] split = level.split(",");
            for (int i=0;i<split.length;i++) {
                if (!TextUtils.isEmpty(split[i])) {
                    ImageView img = new ImageView(getContext());
                    llGif.addView(img);
                    img.setLayoutParams(new LinearLayout.LayoutParams(width, width));
                    Glide.with(getContext()).
                            load(BitmapUtil.getResIdByMipMapFolder(getContext(), split[i])).
                            diskCacheStrategy(DiskCacheStrategy.SOURCE).
                            into(img);
                }
            }
        }
    }

    String getLevel(float user_winmoneys){
        int money = (int)(user_winmoneys);
        if(money > 9999999) {
            money = 9999999;
        }
        String html = "";
        int crownNum = (money / 1000000);
        if(crownNum > 0) {
//            html += crowns[crownNum - 1]+",";
            html += crowns[crownNum]+",";
            money %= 1000000;
        }

        int cupNum = (money / 100000);
        if(cupNum > 0) {
//            html += cups[cupNum - 1]+","; //原始-1得到的是前一個等級
            html += cups[cupNum ]+",";
            money %= 100000;
        }

        int medalNum = (money / 10000);
        if(medalNum > 0) {
//            html += medals[medalNum - 1]+",";
            html += medals[medalNum]+",";
            money %= 10000;
        }

        int starNum = (money / 1000);
        if(starNum > 0) {
//            html += stars[starNum - 1]+",";
            html += stars[starNum]+",";
        }
        return html;
    }

    String[] initLevel(String title,String name){
        String arr[] =new String[10];
        for(int i = 1; i < 9; i++) {
            arr[i] = title + "_"+i;
        }
        return arr;
    }
}
